//
//  LYDiscoverViewController.m
//  artist
//
//  Created by Jay_Apple on 16/2/29.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYDiscoverViewController.h"
#import "LYDiscoveryModel.h"
#import "LYSettingViewController.h"
#import <UMSocial.h>


@interface LYDiscoverViewController ()<UITableViewDataSource, UITableViewDelegate, UMSocialUIDelegate>

@property (nonatomic, strong) NSMutableArray *modelsArray;

@end

@implementation LYDiscoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 网络请求
/**
 *  签到
 */
- (void)sign{
    
    //签到
    [LYRequestTool userSiginWithToken:LYToken success:^{
        
        [MBProgressHUD showSuccess:@"签到成功"];
        
        
    } failure:^(NSString *code, NSString *message) {
        
        [MBProgressHUD showOnlyText:message];
        
    }];
    
    
}


#pragma mark - 懒加载

- (NSMutableArray *)modelsArray{
    
    if (_modelsArray == nil) {
        _modelsArray = [LYDiscoveryModel mj_objectArrayWithFilename:@"discovery.plist"];
    }
    
    return _modelsArray;
}
#pragma mark - tableviewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return LYDiscoveryCellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LYDiscoveryModel *model = self.modelsArray[indexPath.section][indexPath.row];
    
    [[VVYCheckStatus sharedInstance] checkIsLoginBlock:^(LYUserInfoModel *userInfo) {
        
        if ([model.title containsString:@"邀请"]) {
            
            [self share];
            
        }else if ([model.title containsString:@"签到"]){
        
            [[VVYCheckStatus sharedInstance] checkIsLoginBlock:^(LYUserInfoModel *model) {
                
                
                [self sign];
                
            }];
        
        }else{
        
            [[VVYCheckStatus sharedInstance] checkIsLoginBlock:^(LYUserInfoModel *info) {
                Class pushClass = NSClassFromString(model.classStr);
                UIViewController *vc = [[pushClass alloc] init];
                vc.navigationItem.title = model.title;
                [self.navigationController pushViewController:vc animated:YES];
            }];

        }
        
    }];

    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    return [[UIView alloc] init];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return LYDiscoverySectionMargin;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.1;
}

#pragma mark - tableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.modelsArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.modelsArray[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    LYDiscoveryModel *model = self.modelsArray[indexPath.section][indexPath.row];
    

    cell.imageView.image = [UIImage imageNamed:model.imageName];
    cell.textLabel.text = model.title;
    cell.textLabel.font = [UIFont systemFontOfSize:LYDiscoveryCellTextFont];
    cell.textLabel.textColor = [UIColor colorWithHexString:LYDiscoveryCellTextColor alpha:1.0];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
    
}

/**
 *  分享
 */
- (void)share{
    
    [UMSocialData defaultData].extConfig.wechatSessionData.url = SharedLink;
    [UMSocialData defaultData].extConfig.wechatTimelineData.url = SharedLink;
    [UMSocialData defaultData].extConfig.qqData.url = SharedLink;
    [UMSocialData defaultData].extConfig.qzoneData.url = SharedLink;
    [UMSocialData defaultData].extConfig.sinaData.urlResource.url = SharedLink;
    
    NSString *conent = @"人人都是e术家";
    
    
    //如果需要分享回调，请将delegate对象设置self，并实现下面的回调方法
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:UMSocialAppKey
                                      shareText:conent
                                     shareImage:[UIImage imageNamed:@"icon"]
                                shareToSnsNames:@[UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQQ,UMShareToQzone,UMShareToSina]
                                       delegate:self];
}


@end
